<?php
/**
 ***********************************************************************************************************************
 * 快递员基础资料获取
 * @author ldh 2017-4-18
 ***********************************************************************************************************************
 */
namespace soa\partner;

use soa\client;

/**
 * Class Counterman
 * @package service\counterman
 */
class Counterman extends client
{
    /**
     * 测试账号
     *
     * @var array
     */
    private $testers = array(
        '18684898710' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '黄华1',
            'counterman_code' => '47110001',
            'phone'           => '18684898710',
            'role'            => '业务员',
            'branch_code'     => '471100',
            'tel'             => '021-62153711'
        ),
        '13661964640' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '黄ceshi',
            'counterman_code' => '2017000008',
            'phone'           => '13661964640',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '13552554260' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '周ceshi',
            'counterman_code' => '4157008004',
            'phone'           => '13552554260',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '13611889195' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '周ceshi',
            'counterman_code' => '4157008004',
            'phone'           => '13611889195',
            'role'            => '业务员',
            'branch_code'     => '415700',
            'tel'             => '021-62153711'
        ),
        //新加测试账号
        '13671856416' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '周ceshi',
            'counterman_code' => '4157008004',
            'phone'           => '13671856416',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '13661957145' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '周ceshi',
            'counterman_code' => '4157008004',
            'phone'           => '13661957145',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '13336140171' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '周ceshi',
            'counterman_code' => '4157008004',
            'phone'           => '13336140171',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '18650819791' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => '刘ceshi',
            'counterman_code' => '1022082058',
            'phone'           => '18650819791',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '13564440381' => array(
            'shop_name'       => '西藏拉萨公司',
            'counterman_name' => 'Runny',
            'counterman_code' => '2017000008',
            'phone'           => '13661964640',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
        '13524849951' => array(
            'shop_name'       => '上海青浦公司',
            'counterman_name' => '测试',
            'counterman_code' => '2017009108',
            'phone'           => '13524849951',
            'role'            => '业务员',
            'branch_code'     => '201700',
            'tel'             => '021-62153711'
        ),
    );

    /**
     * @var array
     */
    private $testMap = array(
        '47110001'   => '18684898710',
        '2017000008' => '13661964640',
        '2017000009' => '13552554260',
        '4157008004' => '13611889195',
        '1022082058' => '18650819791',
        '2017009108' => '13524849951',
    );

    /**
     * 获取快递员基础信息 支持手机号码、工号
     *
     * @param $data
     * @return array
     */
    public function counterman($data)
    {
        $brand    = !empty($data['brand']) ? $data['brand'] : '';

        $mobile   = !empty($data['mobile']) ? $data['mobile'] : '';
        $mobile   = !empty($data['phone']) ? $data['phone'] : $mobile;

        $mobiles  = !empty($data['mobiles']) ? $data['mobiles'] : '';
        $mobiles  = !empty($data['phones']) ? $data['phones'] : $mobiles;

        $empNo    = !empty($data['emp_no']) ? $data['emp_no'] : '';
        $empNo    = !empty($data['cm_code']) ? $data['cm_code'] : $empNo;

        $empNos   = !empty($data['emp_nos']) ? $data['emp_nos'] : '';
        $empNos   = !empty($data['cm_codes']) ? $data['cm_codes'] : $empNos;

        $branchName = !empty($data['branch_name']) ? $data['branch_name'] : '';
        $branchName = !empty($data['branchName']) ? $data['branchName'] : $branchName;

        $branchCode = !empty($data['branch_id']) ? $data['branch_id'] : '';//网点编号！！！！！！！！！！！
        $branchCode = !empty($data['branch_no']) ? $data['branch_no'] : $branchCode;
        $branchCode = !empty($data['branch_code']) ? $data['branch_code'] : $branchCode;


        // 根据号码获取快递员信息
        if(!empty($mobile)) {
            if(!is_numeric($mobile) || strlen($mobile) != 11) {
                return raise(1009, '手机号码格式错误');
            }

            // 测试账号特殊处理
            if(in_array($mobile, array_keys($this->testers))) {
                return array(
                    $this->testers[$mobile]
                );
            }
            return $this->get('/v1/basicInfo/Counterman/getCmByMobile',['brand'=>$brand,'mobile'=>$mobile]);
        }

        if(!empty($branchName) && empty($branchCode)){
            $branchInfo = $this->get('/v1/BasicInfo/getBranchByName',['brand'=>$brand,'BranchName'=>$branchName]);
            if(!empty($branchInfo['BranchCode'])){
                $branchCode = $branchInfo['BranchCode'];
            }
        }

        //1、手机号
        if(!empty($mobiles)) {
            $mobilesArr = is_array($mobiles) ? $mobiles : explode(',', $mobiles);
            // 测试账号特殊处理
            $testData = array();
            foreach($mobilesArr as $key => $mobile) {
                if(in_array($mobile, array_keys($this->testers))) {
                    $testData[] = $this->testers[$mobile];
                }
            }
            $ret = $this->get('/v1/basicInfo/Counterman/getCmByMobiles',['brand'=>$brand,'mobiles'=>$mobilesArr]);
            return array_merge($ret, $testData);
        }

        // 2、工号
        if(!empty($empNo)) {
            if(strlen($empNo) > 64) {
                return raise(1009, '工号长度过长');
            }
            // 测试账号特殊处理
            if(in_array($empNo, array_keys($this->testMap))) {
                return [$this->testers[$this->testMap[$empNo]]];
            }
            return $this->get('/v1/basicInfo/Counterman/getCmByCode',['brand'=>$brand,'counterman_code'=>$empNo]);
        }

        if(!empty($empNos)) {
            $empNosArr = is_array($empNos) ? $empNos : explode(',', $empNos);
            // 测试账号特殊处理
            $testData = array();
            foreach($empNosArr as $key => $empNo) {
                if(in_array($empNo, array_keys($this->testMap))) {
                    $testData[] = $this->testers[$this->testMap[$empNo]];
                }
            }
            $ret = $this->get('/v1/basicInfo/Counterman/getCmByCodes',['brand'=>$brand,'counterman_codes'=>$empNos]);
            return array_merge($ret, $testData);
        }

        // 3、网点编号
        if(!empty($branchCode)) {
            // 测试账号特殊处理
            $testData = array();
            if($branchCode == 201700) {
                $testData = array_values($this->testers);
            }
            $ret = $this->post('/v1/basicInfo/Counterman/getCmByBranchCode',['brand'=>$brand,'branch_code'=>$branchCode]);
            return array_merge($ret, $testData);
        }

        // 默认返回参数错误
        return raise(1009);
    }
}